<?php

Yii::import('application.models._base.BaseNews');

class FNews extends BaseNews
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	public function behaviors()
	{
	return array('translate'=>'application.extensions.STranslateableBehavior.STranslateableBehavior' );
	}
	public function translate()
	{
	    // List of model attributes to translate
	    //return array('fulltext', 'title'); //Example
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'author' => array(self::BELONGS_TO, 'iUsers', 'created_by'),
			'comments' => array(self::HAS_MANY, 'Comment', 'id', 'condition'=>'tbl_comment.status='.Comment::STATUS_APPROVED, 'order'=>'tbl_comment.create_time DESC'),
			'commentCount' => array(self::STAT, 'Comment', 'post_id', 'condition'=>'status='.Comment::STATUS_APPROVED),
		);
	}
	public static function getArticleByCat($catId, $limit=null, $where='',$order='')
	{
		$limit = ($limit!=null)?$limit:5;
		$order = ($order!='')?$order:'ORDER BY id DESC';
		$sql = "SELECT tc.title, tc.id, tc.images, tc.introtext, tc.alias, tc.catid, tc.*
				FROM tbl_content as tc
				WHERE tc.status=".parent::STATUS_PUBLISHED." AND tc.catid= $catId AND tc.type='post'
				$order
				LIMIT $limit
				OFFSET 0
		";
		return FNews::model()->findAllBySql($sql);
		//return Yii::app()->db->createCommand($sql)->queryAll();
	}
	public static function getArticleByCatAlias($cat_alias1,$cat_alias2='',$cat_alias3='', $limit='',$offset='', $order='',$where='')
	{
		$limit = ($limit!='')?" LIMIT $limit":"";
		$offset = ($offset!='')?" OFFSET $offset":"";
		$order = ($order!='')?$order:" ORDER BY id DESC ";
		//$ww = ($cat_alias2!='' && $cat_alias3!='')?" (cc.alias=:p OR cc.alias=:b OR cc.alias=:c) ":" cc.alias=:p ";
		$sql = "SELECT tc.title, tc.id, tc.images, tc.introtext, tc.alias, tc.catid, tc.*,cc.alias as cat_alias, cc.title as cat_title
				FROM tbl_content as tc
				LEFT JOIN  tbl_categories as cc ON tc.catid = cc.id
				WHERE tc.status=".parent::STATUS_PUBLISHED." AND tc.type='post' AND (cc.alias=:p OR cc.alias=:b OR cc.alias=:c)
				$order
				$limit
				$offset
		";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':p', $cat_alias1);
		$command->bindParam(':b', $cat_alias2);
		$command->bindParam(':c', $cat_alias3);
		return $command->queryAll();
	}
	public static function getDetail($alias)
	{
		$sql = "SELECT tc.title, tc.fulltext
				FROM tbl_content as tc
				WHERE tc.alias=:a AND tc.status=".parent::STATUS_PUBLISHED." 
		";
		$command=Yii::app()->db->createCommand($sql);
		$command->bindParam(':a', $alias);
		return $command->queryRow();
	}
	public static function getOtherNews($alias)
	{
		$sql = "SELECT tc.title, tc.fulltext, cc.alias as cat_alias, tc.created, tc.alias
				FROM tbl_content as tc
				LEFT JOIN  tbl_categories as cc ON tc.catid = cc.id
				WHERE tc.alias!=:a AND tc.status=".parent::STATUS_PUBLISHED." AND tc.type='post'
				ORDER BY tc.id DESC
				LIMIT 5
		";
		$command=Yii::app()->db->createCommand($sql);
		$command->bindParam(':a', $alias);
		return $command->queryAll();
	}
	public static function getTotalsArticleByAlias($cat_alias1,$cat_alias2='',$cat_alias3='',$where='')
	{
		$sql = "SELECT count(tc.id) as total
				FROM tbl_content as tc
				LEFT JOIN  tbl_categories as cc ON tc.catid = cc.id
				WHERE (cc.alias=:p OR cc.alias=:b OR cc.alias=:c) AND type='post' AND status=".parent::STATUS_PUBLISHED; 
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':p', $cat_alias1);
		$command->bindParam(':b', $cat_alias2);
		$command->bindParam(':c', $cat_alias3);
		return $command->queryScalar();
	}
	public static function getIdByAlias($alias)
	{
		$sql = "SELECT id FROM tbl_content WHERE alias=:a";
		$command=Yii::app()->db->createCommand($sql);
		$command->bindParam(':a', $alias);
		return $command->queryScalar();
	}
}